// cors中间件
const whiteList =["http://127.0.0.1:5500","http://localhost:9527",undefined,"http://localhost:5500"]
module.exports= (req,res,next)=>{
    // console.log("cors中间件")
    // 拿到当前请求的origin
    const origin = req.headers["origin"];
    console.log(origin)
    // 针对需要预检的请求
    console.log(req.method)
    if(req.method==='OPTIONS'){
        //设置响应头
        //设置允许的请求方法
        res.header("Access-Control-Allow-Methods",req.headers['access-control-request-method'])
        // 设置允许的请求头
        res.header("Access-Control-Allow-Headers",req.headers['access-control-request-headers'])
        
        
    }
    res.header("Access-Control-Allow-Credentials", true)

    // 针对简单请求
    if(whiteList.includes(origin)){
        //在可访问的域里面
      res.header("access-control-allow-origin",origin)
    }

    next()
    // console.log(origin)
    

  
}